#!/bin/bash
clear
echo Apple TV Crystal HD Linux Installer by Sam Nazarko
echo Version 15.11.10
#vars
ImageBase="http://crystalhd-for-atv.googlecode.com/svn/trunk/image/hardy/"
EndSector="38.9GB"
#wget all the packages
echo Downloading packages...
wget -r -l1 --no-parent -A.img.gz $ImageBase
#prep internal disk for transfer
#zero initial sectors
echo Erasing the internal drive
sudo dd if=/dev/zero of=/dev/sda bs=4096 count=1M
# sync the system disk partition tables
echo Synchronising partition tables
sudo partprobe /dev/sda
# create the GPT format
echo Creating GPT
sudo parted -s /dev/sda mklabel gpt
#create partitions
echo Creating partitions
sudo parted -s /dev/sda mkpart primary HFS 40s 69671s
sudo parted -s /dev/sda set 1 atvrecv on
sudo parted -s /dev/sda mkpart primary linux-swap 69672s 1118247s
sudo parted -s /dev/sda mkpart primary ext3 1118248s $EndSector
#resync partition table
echo Resyncing partition table
sudo partprobe /dev/sda
#Transferring image
echo Transferring images to internal diskette (this may take some time):
echo Transferring Recovery partition
gunzip -c Recovery.img.gz | sudo dd of=/dev/sda1
echo Transferring Swap partition
gunzip -c Swap.img.gz | sudo dd of=/dev/sda2
echo Transferring Root partition
gunzip -c Root.img.gz | sudo dd of=/dev/sda3
#effects of changing /dev/sdb to /dev/sda must be recognised
#copy new com.apple.boot.plist to Recovery
wget "$ImageBase"/com.apple.Boot.plist"
fsck.hfsplus /dev/sda1
mkdir mnt
mount -t hfsplus -o rw /dev/sda1 mnt
sudo cp -r com.apple.Boot.plist mnt/*
sudo umount /dev/sda1
rm -rf mnt
#update fstab (swap file)
wget "$ImageBase"/fstab
#resize file system.
echo Resizing root partition -- uses full internal capacity.
sudo e2fsck -f /dev/sda3
sudo resize2fs /dev/sda3
#print output
df -h
sleep 10000
#success message
clear
echo "Congratulations, Linux is now installed on your internal drive. Please remove the USB and turn off your Apple TV and back on. The system will then configure itself for first-run."
echo "(c) 2010 Sam Nazarko"
#hide penbuntu login
sleep 999999